package com.zzzzzz.account.settings;

import java.io.IOException;

import javax.annotation.Resource;

import org.apache.shiro.authc.credential.PasswordService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.zzzzzz.account.signin.NoSuchOnlineUserException;
import com.zzzzzz.account.signin.OnlineService;
import com.zzzzzz.account.signin.UserPasswordException;
import com.zzzzzz.account.user.User;
import com.zzzzzz.account.user.UserDao;
import com.zzzzzz.common.util.Constants;
import com.zzzzzz.common.util.FileUtils;
import com.zzzzzz.core.data.filestore.FileData;
import com.zzzzzz.core.data.filestore.FileStorage;

@Service("settingsService")
public class SettingsServiceImpl implements SettingsService {
	
	@Resource
	private OnlineService onlineService;
	@Resource
	private PasswordService passwordService;
	@Resource
	private FileStorage fileStorage;
	@Resource
	private UserDao userDao;
	
	public void editPortrait(EditPortraitForm editPortraitForm) throws NoSuchOnlineUserException, MaxUploadSizeExceededException, IOException  {
//		FileData fileData = FileUtils.getFileData(portraitForm.getFiles());
		CommonsMultipartFile files = editPortraitForm.getFiles();
		if(!files.isEmpty()) {
			
			long fileSize = files.getSize();
			if(fileSize > Constants.IMAGE.USER_PORTRAIT_FILE_SIZE){
				throw new MaxUploadSizeExceededException(fileSize);
			}
			
			// remove current portrait and thumbnail
			removePortrait();
			FileData portraitData = FileUtils.getResizedImageData(files, Constants.IMAGE.USER_PORTRAIT_SIDE_LENGTH, Constants.IMAGE.USER_PORTRAIT_SIDE_LENGTH, true);
			String portraitId = fileStorage.storeFile(portraitData);
			String userId = onlineService.findUserId();
			userDao.editPortrait(userId , portraitId);

			FileData thumbnailData = FileUtils.getResizedImageData(files, Constants.IMAGE.USER_THUMBNAIL_SIDE_LENGTH, Constants.IMAGE.USER_THUMBNAIL_SIDE_LENGTH, true);
			String thumbnailId = fileStorage.storeFile(thumbnailData);
			userDao.editThumbnail(userId, thumbnailId);
		}
	}
	
	public void removePortrait() throws NoSuchOnlineUserException {
		User user = onlineService.findUser();
		fileStorage.deleteFileById(user.getPortraitId());
		fileStorage.deleteFileById(user.getThumbnailId());
		userDao.editPortrait(user.getUserId() , "");
		userDao.editThumbnail(user.getUserId() , "");
	}
	
	public void editPassword(String curPassword, String newPassword) throws NoSuchOnlineUserException, UserPasswordException {
		User user = onlineService.findUser();
		if(!passwordService.passwordsMatch(curPassword, user.getPassword())) {
			throw new UserPasswordException(UserPasswordException.PASSWORDS_WRONG);
		}
		
		userDao.editPassword(user.getUserId(), newPassword);
	}
	
	public void editEmail(String curPassword, String email) throws NoSuchOnlineUserException, UserPasswordException {
		User user = onlineService.findUser();
		if(!passwordService.passwordsMatch(curPassword, user.getPassword())) {
			throw new UserPasswordException(UserPasswordException.PASSWORDS_WRONG);
		}
		
		userDao.editEmail(user.getUserId(), user.getEmail());
	}
	
}
